package Classes.GameUtils
{
	import Classes.Managers.M_ConfigData;
	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	/**
	 * @author thy
	 * @version 1.0.0
	 * 创建时间：Apr 27, 2012 11:32:59 AM
	 */
	public class GU_Sound
	{
		private static var _instance:GU_Sound;
		private var _musicConfigDatasObj:Object;
		
		public function GU_Sound()
		{
		}
		
		public static function get instance():GU_Sound
		{
			if (!_instance)
				_instance=new GU_Sound;
			return _instance;
		}
		
		//*****************操作声效*********************
		private var commSound:Sound;
		private var _playCommFlag:Boolean=true;
		private var iSoundC:SoundChannel;
		private var infinitySound:Sound;
		private var playIflag:Boolean=true;
		private var nowSoundName:String="";
		
		/**
		 * 播放声效
		 * @param musicName
		 */
		public function playCommSound(musicName:String):void
		{
			if(_playCommFlag)
			{
				commSound=new Sound();
				var ss:String=M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName);
				var commRequest:URLRequest=new URLRequest(M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName));
				commSound.load(commRequest);
				
				commSound.play();
			}
		}
		
		/**
		 * 停止或者播放游戏音效 
		 */		
		public function stopOrPlayCommSound():void
		{
			if(!_playCommFlag && commSound)
			{
				try{
					commSound.close();
				}catch(e:Error){}
				
			}
			
			if(!_playCommFlag)
				this.whishtCommAll();
			else
				this.playCommAll();
			
			this._playCommFlag=!_playCommFlag;
			
			if(!this.playIflag){
				this.iSoundC.stop();
				this.playIflag		=	true;
			}
			else{
				if(this.nowSoundName!=""){
					this.playInfinitySound(this.nowSoundName);
				}
			}

		}
		
		
		//*****************操作背景音乐*********************
		
		private var bgSound:Sound;
		private var bgSoundChannel:SoundChannel;
		private var _playBgFlag:Boolean=true;
		private var musicVoice:Number=1;
		//		private var playBgPosition:Number=0;
		
		/**
		 * 播放背景音乐
		 * @param musicName
		 */
		public function playBgSound(musicName:String,bufferTimer:int=5000,musicVoice:Number=1):void
		{
			this.musicVoice=musicVoice;
			var bgRequest:URLRequest=new URLRequest(M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName));
			var buffer:SoundLoaderContext=new SoundLoaderContext(bufferTimer);
			
			if(bgSound)
			{
				bgSound=null;
				bgSoundChannel.stop();
			}
			
			bgSound=new Sound();
			
			bgSound.load(bgRequest, buffer);
			
			if(_playBgFlag)
			{
				bgSoundChannel=bgSound.play(0, int.MAX_VALUE);
				var transform:SoundTransform = new SoundTransform( );
				transform.volume = musicVoice;
				bgSoundChannel.soundTransform = transform;
			}
		}
		
		/**
		 *  暂停或者开启背景音乐
		 */
		public function puaseOrPlayBgSound():void
		{
			if (_playBgFlag)
			{
				//				playBgPosition=bgSoundChannel.position;
				bgSoundChannel.stop();
			}
			else
			{
				bgSoundChannel=bgSound.play(0, int.MAX_VALUE);
				var transform:SoundTransform = new SoundTransform( );
				transform.volume = musicVoice;
				bgSoundChannel.soundTransform = transform;
				//				playBgPosition=0;
			}
			_playBgFlag=!_playBgFlag;
		}
		
		/**
		 * 停止所有音乐 
		 * @param event
		 */
		public function stopAllSounds():void
		{
			SoundMixer.stopAll();
			this.stopAllIS();
		}
		
		
		public function get itemConfigDatasObj():Object
		{
			if (!_musicConfigDatasObj)
			{
				_musicConfigDatasObj=new Object;
				var musicConfigDatasStr:String=M_ConfigData.instance.getCSVByName("音乐配置表");
				var musicConfigDatasAry:Array=GU_DataTransform.csvTransformObjsAry(musicConfigDatasStr, 3);
				for each (var musicConfigDataObj:Object in musicConfigDatasAry)
				{
					_musicConfigDatasObj[musicConfigDataObj.soundName]=musicConfigDataObj;
				}
			}
			return _musicConfigDatasObj;
		}
		
		/**
		 * 根据名字 得到 music 地址
		 * @param musicname
		 * @return
		 */
		public function getMusicUrlByName(musicname:String):String
		{
			return itemConfigDatasObj[musicname].soundUrl;
		}
		
		public function get playBgFlag():Boolean
		{
			return this._playBgFlag;
		}
		
		public function get playCommFlag():Boolean
		{
			return this._playCommFlag;
		}
		
		//=================================================永久播放的音效==================================//
		/**
		 *永久播放的音效 
		 * @param musicName
		 */
		public function playInfinitySound(musicName:String):void
		{
			this.nowSoundName	=	musicName;
			if(playIflag && _playCommFlag){
				infinitySound=new Sound();
				var ss:String=M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName);
				var commRequest:URLRequest=new URLRequest(M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName));
				infinitySound.load(commRequest);
				
				iSoundC=infinitySound.play(0,int.MAX_VALUE);
				playIflag=false;
			}
		}
		/**
		 *关闭永久播放音效 
		 */
		public function closeInfinitySound():void
		{
			if(!playIflag){
				iSoundC.stop();
				playIflag=true;
			}
			this.nowSoundName="";
		}
		
		private var iSoundObj:Object= new Object();
		private var iSoundNameAry:Array=new Array;
		public function playISound(musicName:String):void
		{
			if(_playCommFlag){
				var sound:Sound		=	new Sound();
				var ss:String=M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName);
				var commRequest:URLRequest=new URLRequest(M_ConfigData.instance.musicPath + this.getMusicUrlByName(musicName));
				sound.load(commRequest);
				
				iSoundObj[musicName]=sound.play(0,int.MAX_VALUE);
				iSoundNameAry.push(musicName);
				
			}
		}
		
		public function stopISound(musicName:String):void
		{
			if(!iSoundObj.hasOwnProperty(musicName) || iSoundObj[musicName]==null)return;
			
			(iSoundObj[musicName] as SoundChannel).stop();
			iSoundObj[musicName]	=	null;
			iSoundNameAry.splice(iSoundNameAry.indexOf(musicName),1);
			
		}
		/**
		 *停止所有播发的声效-------循环的 
		 * 
		 */
		public function stopAllIS():void
		{
			for each(var item:SoundChannel in iSoundObj){
				item.stop();
			}
		}

		
		/**
		 *静音-----循环播放的 
		 * 
		 */
		public function whishtCommAll():void
		{
			var transform:SoundTransform 	=	new SoundTransform();
			transform.volume	=	0;
			for each(var item:SoundChannel in iSoundObj){
				if(item)
				item.soundTransform		=	transform;
			}
		}
		
		/**
		 *还原静音 
		 * 
		 */
		public function playCommAll():void
		{
			var transform:SoundTransform 	=	new SoundTransform();
			transform.volume	=	1;
			for each(var item:SoundChannel in iSoundObj){
				if(item)
				item.soundTransform		=	transform;
			}
		}
	}
}
